/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.mailfolder;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.common.util.TimeUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.mailfolder.Mail;
import com.unis.model.mailfolder.Mailbox;
import com.unis.service.attach.AttachLinkService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-02-21
 */
@Service("mailService")
public class MailServiceImpl implements MailService {

    private IDataSet4iBatis iDataSet4iBatis;
    private AttachLinkService attachlinkService;

    private MailboxService mailboxService;

    /****************************************
     * Insert a instace of Mail
     ****************************************/
    public void insert(Mail mail) throws Exception {
        mail.setMailid(TemplateUtil.genUUID());
        mail.setSendtime(TimeUtil.getNow());
        iDataSet4iBatis.insert(mail);
        int status = mail.getMailstatus();
        Mailbox mailbox = new Mailbox();
        // 邮件表与附件表进行关联 如果有多个附件则循环插入
        if (!"".equals(mail.getFileids())) {
            AttachLink attachlink = new AttachLink();
            attachlink.setStatmentId("AttachLink.insert");
            attachlink.setEntityid(mail.getMailid());
            String[] fileids = mail.getFileids().split(",");
            for (int i = 0; i < fileids.length; i++) {
                attachlink.setFileid(fileids[i]);
                attachlinkService.insert(attachlink);
            }
        }
        if (status == 0) {
            //判断邮件是否为草稿，若是，置入草稿箱
            String mailid = mail.getMailid();
            mailbox.setMailid(mailid);
            mailbox.setFolderid("3");
            mailbox.setUserid(mail.getSenderid());
            mailbox.setSendtime(mail.getSendtime());
            mailbox.setDelflag(0);
            mailbox.setReadflag(mail.getReadflag());
            mailbox.setReplyflag(mail.getReplyflag());
            mailbox.setStatmentId("Mailbox.insert");
            mailboxService.insert(mailbox);
        } else {
            //判断邮件是否为草稿，若不是，置入发件箱与收件箱
            String mailid = mail.getMailid();
            mailbox.setMailid(mailid);
            mailbox.setFolderid("2");
            mailbox.setUserid(mail.getSenderid());
            mailbox.setSendtime(mail.getSendtime());
            mailbox.setDelflag(0);
            mailbox.setReadflag(mail.getReadflag());
            mailbox.setReplyflag(mail.getReplyflag());
            mailbox.setStatmentId("Mailbox.insert");
            mailboxService.insert(mailbox);

            mailbox.setUserid(mail.getRecipientids());
            mailbox.setFolderid("1");
            mailboxService.insert(mailbox);
        }
    }

    /****************************************
     * Update a instace of Mail
     ****************************************/
    public void update(Mail mail) throws Exception {
        mail.setSendtime(TimeUtil.getNow());
        if (mail.getDelflag() == 1) {
            mail.setDelflag(0);
            Mailbox mailbox = new Mailbox();
            mailbox.setStatmentId("Mailbox.delete");
            mailbox.setUserid(mail.getUserid());
            mailbox.setMailid(mail.getMailid());
            if (mail.getFoldertype() == 4) {
                mailbox.setDelflag(1);
                mailbox.setFolderid("5");
                mailboxService.update(mailbox);
            } else {
                mailbox.setFolderid("4");
                mailboxService.update(mailbox);
            }

        } else {
            iDataSet4iBatis.update(mail);
            Mailbox mailbox = new Mailbox();
            mailbox.setStatmentId("Mailbox.update");
            mailbox.setUserid(mail.getUserid());
            mailbox.setMailid(mail.getMailid());
            mailbox.setSendtime(mail.getSendtime());
            mailbox.setFolderid("2");
            mailboxService.update(mailbox);

            mailbox.setUserid(mail.getRecipientids());
            mailbox.setFolderid("1");
            mailbox.setStatmentId("Mailbox.insert");
            mailboxService.insert(mailbox);
        }
    }

    /****************************************
     * Delete a instace of Mail
     ****************************************/
    public void delete(Mail mail) throws Exception {
        iDataSet4iBatis.delete(mail);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Mail mail) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Mail mail) throws Exception {
        String mainId = mail.getId();

        Mailbox mailbox = new Mailbox();
        mailbox.setStatmentId("Mailbox.update");
        mailbox.setId(mainId);
        mailbox.setUserid(mail.getUserid());
        mailbox.setFolderid(String.valueOf(mail.getFoldertype()));

        mailboxService.batchUpdate(mailbox);
    }

    /****************************************
     * Update instaces of Mail
     ****************************************/
    public void batchUpdate(Mail mail) throws Exception {
        String statmentId = mail.getStatmentId();
        String mainId = mail.getId();
        String userId = mail.getUserid();
        String[] ids = mainId.split(",");
        List<BaseModel> mailList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Mail m = new Mail();
            m.setStatmentId(statmentId);
            m.setMailid(id);
            m.setUserid(userId);
            mailList.add(m);
        }
        iDataSet4iBatis.batchUpdate(mailList);
    }

    /****************************************
     * Query instaces of Mail
     ****************************************/
    public List<BaseModel> queryAsList(Mail mail) throws Exception {
        String queryListStatement = "";
        if (mail.isPaging()) {
            queryListStatement = mail.getStatmentId();
            mail.setStatmentId("Mail.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(mail);
            mail.setTotalCount(baseModel.getTotalCount());
            mail.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(mail);
    }

    /****************************************
     * Query a instaces of Mail
     ****************************************/
    public BaseModel queryAsObject(Mail mail) throws Exception {
        return iDataSet4iBatis.queryAsObject(mail);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }

    @Resource
    public void setMailboxService(MailboxService mailboxService) {
        this.mailboxService = mailboxService;
    }
    
    @Resource
    public void setAttachlinkService(AttachLinkService attachlinkService) {
        this.attachlinkService = attachlinkService;
    }

}
